<template>
  <div class="title">轮播图管理</div>
  <div class="content">
    <div class="search-bar flex-aic">
      <span style="margin-right: 14px">位置: </span>
      <el-select
          v-model="bannsersParams.addr"
          placeholder="请选择位置"
          size="large"
          style="width: 240px"
      >
        <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
        />
      </el-select>

      <JSL_BTN style="margin:0 14px;" type="primary" text="搜索" @click="handleSizeChange"/>
      <JSL_BTN @click="toAdd" text="添加轮播图"/>
    </div>

    <el-table
        :data="tableData"
        style="width: 100%"
        border stripe
        :header-cell-style="{ textAlign: 'center', background: '#EEEEEE', borderColor: '#DBDBDB'}"
        :cell-style="{ textAlign: 'center', borderColor: '#DBDBDB' }"
    >
      <el-table-column prop="addr" label="位置" width="150" />
      <el-table-column label="图片" width="442">
        <template #default="scope">
          <el-image :src="scope.row.img" style="width: 278px; height: 66px;" lazy/>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="链接"/>
      <el-table-column prop="sort" label="排序" width="180"/>
      <el-table-column prop="createTime" label="添加时间" width="240" />
      <el-table-column label="操作" width="280">
        <template #default="scope">
          <div class="table-tool flex-aic flex-juc">
            <span>编辑</span>
            <span @click="delBanner(scope.row.id)">删除</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
          v-model:current-page="bannsersParams.page"
          v-model:page-size="bannsersParams.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
  </div>

  <el-dialog
      v-model="delDiaalogshow"
      width="300"
  >
    <div class="dialog">
      <div class="text">确认删除此轮播图吗?</div>
      <div class="btns flex-aic flex-jub">
        <JSL_BTN type="clean" size="min" text="取消" class="btn" @click="closeDialog"/>
        <JSL_BTN type="primary" size="min" text="确认" class="btn" @click="delBannerHandel"/>
      </div>
    </div>
  </el-dialog>
</template>


<script setup>
import {ref, reactive} from 'vue'
import {bannerAddrSelections, apiBanners, apiDelbanner} from '@/api/poster'
import { ElMessage } from 'element-plus'
import {useRouter} from 'vue-router'

//位置下拉框
const options = ref([])
const getbannerSelections = async ()=> {
  const res = await bannerAddrSelections()
  options.value = res.list
}
getbannerSelections()

//列表
const tableData = ref([])
const total = ref(0)
//banner列表请求参数
const bannsersParams = reactive({
  page: 1,
  pageSize: 10,
  addr: ''
})
//获取banner列表
const getBanners = async ()=> {
  const res = await apiBanners(bannsersParams.addr, bannsersParams.page, bannsersParams.pageSize)
  tableData.value = [...tableData.value, ...res.list]
  total.value = res.total
}
getBanners();
//分页大小改变或者搜索， 因为select框绑定的值是bannsersParams中的addr，所以搜索可以直接用这个函数
const handleSizeChange = (val = 10) => {
  bannsersParams.page = 1
  bannsersParams.pageSize = val
  tableData.value = []
  getBanners()
}
//页码改变
const handleCurrentChange = (val) => {
  bannsersParams.page = val;
  getBanners()
}


//删除弹窗显示
const delDiaalogshow = ref(false)
let waitDelBannerID = '' //删除的id（临时保存， 使用后清空）
//删除按钮点击
const delBanner = (id)=> {
  waitDelBannerID = id
  delDiaalogshow.value = true
}
//关闭弹窗
const closeDialog = ()=> {
  waitDelBannerID = ''
  delDiaalogshow.value = false
}
//发送删除请求，重新请求列表数据
const delBannerHandel = async ()=> {
  const res = await apiDelbanner(waitDelBannerID);
  delDiaalogshow.value = false
  ElMessage({
    message: res.msg,
    type: 'success',
  })
  handleSizeChange()
}
//去添加页面
const router = useRouter();
const toAdd = ()=> {
  router.push('/poster/add')
}
</script>


<style scoped lang="scss">
@import '@/assets/style/var.scss';

.title{
  font-size: 16px;
  font-weight: 500;
  color: $main-color;
  margin-bottom: 20px;
}
.content{
  width: 100%;
  border-radius: 10px;
  background: #fff;
  padding: 20px 20px 20px 32px;
  .search-bar{
    margin-bottom: 20px;
  }
}
.table-tool{
  width: 100%;
  span{
    margin: 0 10px;
    color: $main-color;
    cursor: pointer;
  }
}
.pagination{
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-top: 20px;
}

.dialog{
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  .text{
    margin: 50px 0;
  }
  .btns{
    padding: 0 20px;
    width: 100%;
  }
}
</style>

